#!/usr/bin/env python
#coding=utf8

from . import Enum

STATUS_CODE = Enum(
    SUCCESS = 200,
    REDIRECT = 302,
    BAD_REQUEST = 400,
    UNAUTHORIED = 401,
    FORBIDDEN = 403,
    NOT_FOUND = 404,
    INTERNAL_ERROR = 500,
    GATEWAY_ERROR = 502,
    SERVER_DOWN = 503,
    GATEWAY_TIMEOUT = 504,

    SUBMAIL_ERROR = 2000
)


class ServiceError(Exception):

    def __init__(self,  code=None, message='', connector_name='', ext_error=''):
        super(ServiceError, self).__init__(code, message)
        self.code = code or self._get_code_by_connector(connector_name)
        self.message = message
        if ext_error:
            self.message += "(code={0}, message={1})".format(ext_error.get("code"), ext_error.get("message"))

    def __repr__(self):
        if hasattr(self, "ext_error"):
            return "(%s, %s)" % (self.ext_error.message, self.message)
        return self.message

    def _get_code_by_connector(self, connector_name):
        if connector_name == 'SubmailConnector':
            return STATUS_CODE.SUBMAIL_ERROR
        else:
            return STATUS_CODE.INTERNAL_ERROR
